﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Film8.DAL;
using Film8.Models;

namespace Film8.Controllers
{
    [Authorize]
    public class FanController : Controller
    {
        private Film8Context db = new Film8Context();

        // GET: Fan
        public ActionResult Index(string searchString, string searchStringAddress, string SearchStringID)
        {
            var fans = from s in db.Fans
                           select s;
            if (!String.IsNullOrEmpty(searchString))
            {
                fans = fans.Where(s => s.Name.ToUpper().Contains(searchString.ToUpper()));
                                      
            }
            if (!String.IsNullOrEmpty(searchStringAddress))
            {
                fans = fans.Where(s => s.Address.ToUpper().Contains(searchStringAddress.ToUpper()));

            }
            if (!String.IsNullOrEmpty(SearchStringID))
            {
                fans = fans.Where(s => s.Id.ToString().ToUpper().Contains(SearchStringID.ToUpper()));

            }
          

            if (Request.IsAjaxRequest())
            {
                return PartialView("_FansTable", fans);
            }
           
            return View(fans);
        }

        // GET: Fan/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Fan fan = db.Fans.Find(id);
            if (fan == null)
            {
                return HttpNotFound();
            }
            return View(fan);
        }

        // GET: Fan/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Fan/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Name,Address")] Fan fan)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Fans.Add(fan);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch (Exception)
            {

               
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }
            

            return View(fan);
        }
         [Authorize(Users = "admin@film8.com")]
        // GET: Fan/Edit/5
        public ActionResult Edit(int? id)
        {

            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Fan fan = db.Fans.Find(id);
            if (fan == null)
            {
                return HttpNotFound();
            }
            return View(fan);
        }

        // POST: Fan/Edit/5
      [Authorize(Users = "admin@film8.com")]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Name,Address")] Fan fan)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Entry(fan).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
           
            }
            catch (Exception)
            {

                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }
            return View(fan);
        }
         [Authorize(Users = "admin@film8.com")]
        // GET: Fan/Delete/5
        public ActionResult Delete(int? id, bool? saveChangesError=false)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Fan fan = db.Fans.Find(id);
            if (fan == null)
            {
                return HttpNotFound();
            }
            return View(fan);
        }

         [Authorize(Users = "admin@film8.com")]
        // POST: Fan/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id)
        {
            try
            {
            Fan fan = db.Fans.Find(id);
            db.Fans.Remove(fan);
            db.SaveChanges();
            
            }
            catch (Exception)
            {

                return RedirectToAction("Delete", new { id = id, saveChangesError = true });
            }
            return RedirectToAction("Index");
           
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
